package org.uim.cricket.entities.parser.filters.impl;

import java.util.Calendar;

import org.uim.cricket.entities.FullPlayer;
import org.uim.cricket.entities.parser.filters.PlayerDetailFilterable;


public class DefaultPlayerDetailFilter implements PlayerDetailFilterable {

	@Override
	public void filterAttribute(FullPlayer player, String attrName, String attrValue) {
		if (attrValue == null) return;
		if (attrName.equalsIgnoreCase("Born")) {
			String[] parts = attrValue.split("[,]");
			if (parts.length >= 2) {
				player.setBirthdate(this.parseDate(parts[0]+","+parts[1]));
			}
		} else if (attrName.equalsIgnoreCase("Batting style")) {
			int pos = attrValue.indexOf('-');
			if (pos >=0) {
				String hand = attrValue.substring(0, pos).toLowerCase();
				player.addNewAttribute(attrName, hand);
			}
		} else if (attrName.equalsIgnoreCase("Major teams")) {
			String[] teams = attrValue.split("[,]");
			if (teams != null && teams.length > 0) {
				for (int i=0;i<teams.length;i++) {
					player.addRepresentedCountry(teams[i].trim());
				}
			}
		} else if (attrName.equalsIgnoreCase("Height")) {
			int ft = attrValue.indexOf("ft");
			int in = attrValue.indexOf("in");
			if (ft >= 0 && in >= 0) {
				int f = Integer.parseInt(attrValue.substring(0, ft).trim());
				int inc = Integer.parseInt(attrValue.substring(ft+2,in).trim());
				player.addNewAttribute(attrName, (12 *f + inc) + "");
			}
		} else if (attrName.equalsIgnoreCase("Died")) {
			String[] parts = attrValue.split("[,]");
			if (parts.length >= 2) {
				player.addNewAttribute(attrName, parts[0] + "," + parts[1]);
			} else {
				player.addNewAttribute(attrName, parts[0]);
			}
		} else if (attrName.equalsIgnoreCase("Current age")) {
			/* do nothing */
		} else if (attrName.equalsIgnoreCase("Full name")) {
			player.setFullName(attrValue);
		} else {
			player.addNewAttribute(attrName, attrValue);
		}
		
	}
	
	private Calendar parseDate(String text) {
		if (text == null) return Calendar.getInstance();
		Calendar c = Calendar.getInstance();
		
		String[] parts = text.split("[,]");
		if (parts.length < 2) return c;
		
		String[] subparts = parts[0].split("[ ]");
		if (subparts.length < 2) return c;
		
		int year,month,day;
		month = this.getMonth(subparts[0]);
		day = Integer.parseInt(subparts[1].trim());
		year = Integer.parseInt(parts[1].trim());
		
		c.set(year, month, day);
		
		return c;
	}
	
	private int getMonth(String month) {
		String prefix = month.toLowerCase().substring(0, 3);
		if (prefix.equals("jan")) return Calendar.JANUARY;
		else if (prefix.equals("feb")) return Calendar.FEBRUARY;
		else if (prefix.equals("mar")) return Calendar.MARCH;
		else if (prefix.equals("apr")) return Calendar.APRIL;
		else if (prefix.equals("may")) return Calendar.MAY;
		else if (prefix.equals("jun")) return Calendar.JUNE;
		else if (prefix.equals("jul")) return Calendar.JULY;
		else if (prefix.equals("aug")) return Calendar.AUGUST;
		else if (prefix.equals("sep")) return Calendar.SEPTEMBER;
		else if (prefix.equals("oct")) return Calendar.OCTOBER;
		else if (prefix.equals("nov")) return Calendar.NOVEMBER;
		else if (prefix.equals("dec")) return Calendar.DECEMBER;
		else
			return -1;
    }

}
